package com.ssyt.ec.sys.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.ssyt.ec.common.AppServlet;
import com.ssyt.ec.common.PageModel;
import com.ssyt.ec.common.util.DBUtil;
import com.ssyt.ec.common.util.FastjsonUtil;
import com.ssyt.ec.sys.entity.ClassManagerEntity;

@WebServlet("/ClassManagerServlet")
public class ClassManagerServlet extends AppServlet {

	private static final long serialVersionUID = 1L;
	//private ClassManagerService classService = new ClassManagerService();

	public ClassManagerServlet() {
		super();
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		String command = request.getParameter("command");
		if ("listClassManager".equals(command)) {
			listClassManager(request, response);
		} else if ("addClassManager".equals(command)) {
			addClassManager(request, response);
		} else if ("getClassManager".equals(command)) {
			getClassManager(request, response);
		} else if ("updateClassManager".equals(command)) {
			updateClassManager(request, response);
		} else if ("deleteClassManager".equals(command)) {
			deleteClassManager(request, response);
		} 
	}

	/**
	 * 查询班级信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void listClassManager(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		String sort = request.getParameter("sort");
		String order = request.getParameter("order");
		String name = request.getParameter("name");
		//int state = Integer.parseInt(request.getParameter("state"));

		List<Object> paramList = new ArrayList<Object>();
		StringBuilder sb = new StringBuilder("SELECT cl.*,co.name as 'course__name' FROM sys_class cl,sys_course co WHERE co.id=cl.course_id ");
		if (StringUtils.isNotBlank(name)) {
			sb.append("AND cl.name LIKE ? ");
			paramList.add("%" + name + "%");
		}
		sb.append("ORDER BY " + sort + " " + order + " LIMIT ?,? ");
		paramList.add((page - 1) * rows);
		paramList.add(rows);
		
		PageModel<ClassManagerEntity> classes = DBUtil.executeQueryByPage(
				ClassManagerEntity.class, sb.toString(), paramList);
		print(response, FastjsonUtil.toJson(classes));
	}
	
	/**
	 * 新增班级
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void addClassManager(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
        
		String name = request.getParameter("name");
        int courseId = Integer.parseInt(request.getParameter("courseId"));
        int operateUserId = Integer.parseInt(request.getParameter("operateUserId"));
        String operateTime = request.getParameter("operateTime");
        int state = Integer.parseInt(request.getParameter("state"));
        String memo = request.getParameter("memo");
        int row = DBUtil.executeUpdate("INSERT INTO sys_class VALUES(NULL, ?, ?, ?, ?, ?, ?)",name,courseId,operateUserId,operateTime,state,memo);
        if (row == 1) {
            print(response, 1);
        }
	}
	
	/**
	 * 获取班级信息
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	 private void getClassManager(HttpServletRequest request,
			 HttpServletResponse response) throws IOException {
	        
		 	int id = Integer.parseInt(request.getParameter("id"));
	        ClassManagerEntity classes = DBUtil.getUniqueResult(ClassManagerEntity.class, "SELECT * FROM sys_class WHERE id=?", id);
	        if (classes != null) {
	            print(response, FastjsonUtil.toJson(classes));
	        }
	   }
	 
	/**
	 * 修改班级信息
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void updateClassManager(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
	      
		int id = Integer.parseInt(request.getParameter("id"));
	    String name = request.getParameter("name");
	    int courseId = Integer.parseInt(request.getParameter("courseId"));
	    int state = Integer.parseInt(request.getParameter("state"));
	    String memo = request.getParameter("memo");
	      
	    int row = DBUtil.executeUpdate("UPDATE sys_class SET name=?,course_id=?,state=?,memo=? WHERE id=?", name, courseId, state, memo, id);

	      if (row == 1) {
	          print(response, 1);
	      }
	 }
	
	 /**
	  * 删除班级
	  */
	 private void deleteClassManager(HttpServletRequest request,
			 HttpServletResponse response) throws IOException {
	        
		 String ids = request.getParameter("ids");
	        int row = DBUtil.executeUpdate("DELETE FROM sys_class WHERE id IN (" + ids + ")");

	        if (row >= 1) {
	            print(response, 1);
	       }
	 }
	 
}

